<template>
  <Excel ref="excelRef" :select="excelSelect" @load="excelLoad">
    <el-alert :title="plugin.lang('order.excel.alert')" type="warning" :closable="false" style="margin-bottom: 20px" />
  </Excel>
</template>

<script>
import { ref } from 'vue'
import app from '@/app'
import Excel from '@/components/excel'
import payModel from '@/plugin/config/model/pay'
import model from '../model'

export default {
  components: {
    Excel,
  },
  setup() {
    // 插件
    const plugin = app.plugin.index
    // 表格
    const excelRef = ref()

    // 打开
    const open = () => {
      excelRef.value.open()
    }

    // 支付方式整理
    const pay = {}
    for (let i = 0; i < payModel.getList().length; i++) {
      // 当前支付
      const res = payModel.getList()[i]
      pay['pay-' + res.id] = {
        label: res.name,
        render: data => Number(data['pay-' + res.id] ?? 0)
      }
    }
    // 表格可导出项
    const excelSelect = {
      time: {
        label: plugin.lang('order.time'),
        render: data => data.time
      },
      ...pay,
      money_tax: {
        label: plugin.lang('order.money_tax'),
        render: data => Number(data.money_tax ?? 0)
      },
      tax: {
        label: plugin.lang('order.tax'),
        render: data => Number(data.tax ?? 0)
      },
      tax_money: {
        label: plugin.lang('order.tax_tax'),
        render: data => Number(data.tax_money ?? 0)
      },
      req_money: {
        label: plugin.lang('order.tax_req'),
        render: data => Number(data.req_money ?? 0)
      },
      total: {
        label: plugin.lang('order.total'),
        render: data => Number(data.total ?? 0)
      },
      profit: {
        label: plugin.lang('order.profit'),
        render: data => Number(data.profit_money ?? 0)
      },
      count: {
        label: plugin.lang('order.count'),
        render: data => Number(data.count ?? 0)
      }
    }

    // 表格加载方法
    const excelLoad = () => {
      let i = 0
      // 时间按天分割
      const time = app.time.splitStartEnd([
        app.time.load(model.where.time_arr[0]), 
        app.time.load(model.where.time_arr[1])
      ], 0, 'day')

      excelRef.value.load(async() => {
        // 请求数据
        const data = await model.day(time[i])
        i++
        // 导出原数据
        return [time.length, [data]]
      })
    }

    return {
      plugin,
      open,
      excelRef,
      excelLoad,
      excelSelect
    }
  }
}
</script>